<?php

/*
 * Main installation object
 */

class install{
	private $System	=null;
	private $lang = null;
	private $dboengine = null;
	private $dboserver = null;
	private $dbodb = null;
	private $dbouser = null;
	private $dbopassword = null;
	private $sysname = null;
	private $syslang = null;
	private $sysurl	 = null;
	private $adminemail = "admin@localhost";
	private $adminpassword = "admin";
	private $dbolink = null;
	private $accepted = false;
		
	function __construct(ginit & $Gepard,$lang=null){
		$this->System = &$Gepard;
		if( is_object($lang) && get_class($lang)=="gi18n_image"){
                    $this->lang = $lang;
                }
	}
	
	public function checkconf($dboengine,$dboserver,$dbodb,$dbouser,$dbopassword,$sysname,$syslang,$sysurl,$adminemail,$adminpassword){
		if( !preg_match("/^[^@ ]+@[^@ ]+\.[^@ ]+/",$adminemail) ){
			return $this->lang->get("error5");
		} else if ( strlen($adminpassword)<5 ){
			return $this->lang->get("error6");
		} else if( !in_array($syslang,$this->System->get("syslanguages")) ){
			return  $this->lang->get("error7");
		} else {
		
		//$dbo=$this->System->dbo->load($dboengine,array("server"=>$dboserver,"db"=>$dbodb,"user"=>$dbouser,"password"=>base64_encode($dbopassword)));
		//if( !$dbo ){
		//		$error = $this->System->errors->last();
		//		return $error["SOURCE"]." ".$error["DESCRIPTION"];			
		//} else {

			#accepting configuration
			$this->dboengine = $dboengine;
			$this->dboserver = $dboserver;
			$this->dbodb	= $dbodb;
			$this->dbouser	= $dbouser;
			$this->dbopassword = $dbopassword;
			$this->sysname	= $sysname;
			$this->syslang = $syslang;
			$this->sysurl  = $sysurl;
			$this->adminemail = $adminemail;
			$this->adminpassword = $adminpassword;
			$this->accepted = true;
		//}
		}
		//$this->dbolink = & $this->System->dbo->$dboengine;
		return true;
	}	


	public function checkglobalconsole(){
		$path = $this->System->path();
		$error = null;
		$conffile = $this->System->parsepathtosys($this->System->get("sysappconf"));
		$logfile  = $this->System->parseasdir($this->System->get("logsdir"));
		$savefile = $this->System->parseasdir($this->System->get("uploadsdir"));
		if( !is_writable($conffile) ) $error .=$this->lang->get("error2")." ($conffile )<br>";
		if( !is_writable($logfile) ) $error .=$this->lang->get("error3")." ($logfile)<br>";
		if( !is_writable($savefile) ) $error .=$this->lang->get("error11")." ($savefile) <br>";		
		//if( !isset($_SERVER["REQUEST_URI"])) $error .=$this->lang->get("error4")."<br>";
		//if( !isset($_COOKIE) || !isset($_POST) || !isset($_GET)) $error.=$this->lang->get("error9")."<br>";
		if( !extension_loaded("zlib") || !extension_loaded("gd")  )  $error.=$this->lang->get("error10")."<br>";
		return ( is_null($error)?true:$error);
	}
	
	public function checkglobal(){
		$path = $this->System->path();
		$error = null;
				
		$conffile = $this->System->parsepathtosys($this->System->get("sysappconf"));
		$logfile  = $this->System->parseasdir($this->System->get("logsdir"));
		$savefile = $this->System->parseasdir($this->System->get("uploadsdir"));
		if( !is_writable($conffile) ) $error .=$this->lang->get("error2")." ($conffile )<br>";
		if( !is_writable($logfile) ) $error .=$this->lang->get("error3")." ($logfile)<br>";
		if( !is_writable($savefile) ) $error .=$this->lang->get("error11")." ($savefile) <br>";		
		if( !isset($_SERVER["REQUEST_URI"])) $error .=$this->lang->get("error4")."<br>";
		if( !isset($_COOKIE) || !isset($_POST) || !isset($_GET)) $error.=$this->lang->get("error9")."<br>";
		if( !extension_loaded("zlib") || !extension_loaded("gd")  )  $error.=$this->lang->get("error10")."<br>";
		return ( is_null($error)?true:$error);
	}
	
	public function writefiles(){
		if( !$this->accepted)return false;
		$time = microtime(1);
		$sysconf = $this->System->path().$this->System->get("sysappconf");
		$sysline = "#Gepard ".$this->System->GetVersion()." configuration file\n# auto generated, ".date("Y-m-d H:i:s");
		
		# system configuration
		$file = fopen($sysconf,"w+");
		fwrite($file,"<?php\n$sysline\n\$Gepard->set(\"name\",\"".$this->sysname."\");\n\$Gepard->set(\"language\",\"".$this->syslang."\");\n\$Gepard->set(\"url\",\"".$this->sysurl."\");\n# Database configuration\n\$Gepard->set(\"dboconf\", array(\"".$this->dboengine."\"=>array(\"server\"=>\"".$this->dboserver."\",\"port\"=>\"\",\"db\"=>\"".$this->dbodb."\",\"user\"=>\"".$this->dbouser."\",\"password\"=>\"".base64_encode($this->dbopassword)."\")) ); \n?>");
		fclose($file);

		$this->System->DEBUG("INSTALL","Sys files created in ".round(microtime(1)-$time,4) );
		return true;
	}
	
	public function sqlinstall($options=null){
		$connected= $this->System->dbo->getConnected();

		if(!$this->accepted && !$connected){
			print "Not accepted";
			return false;
		}
		/*
		 * Connectiong to db
		 */
                if( !$connected){
                    $c = array("server"=>$this->dboserver,"db"=>$this->dbodb,"user"=>$this->dbouser,"password"=>base64_encode($this->dbopassword));
                    $drv = $this->dboengine;
                    $this->System->dbo->load($drv,$c);
                    $db = & $this->System->dbo->$drv;
                } else {
                    $db = &$this->System->dbo;
                }

		$this->tablesinstall($db);
                

		/*
		 * Inserting basic parameters
		 */
		$db->insert("groups",array("id"=>"1","name"=>"Administrator","createdate"=>date("Y-m-d H:i:s") ) );
		$db->insert("groups",array("id"=>"2","name"=>"General","createdate"=>date("Y-m-d H:i:s") ) );

		$db->insert("levels",array("id"=>"1","name"=>"DEFAULT"));
		$db->insert("levels",array("id"=>"2","name"=>"ADMIN_PAGE"));
	
		$db->update("levels","set id='0' where name='DEFAULT'");
		$db->update("levels","set id='1' where name='ADMIN_PAGE'");
		/*
		 * Creating Output Areas
		 */
		$db->insert("output_areas",array("name"=>"1","value"=>"main") );
		$db->insert("output_areas",array("name"=>"2","value"=>"left") );
		$db->insert("output_areas",array("name"=>"3","value"=>"right") );
		$db->insert("output_areas",array("name"=>"4","value"=>"top") );
		$db->insert("output_areas",array("name"=>"5","value"=>"menu") );
		$db->insert("output_areas",array("name"=>"6","value"=>"road") );
		$db->insert("output_areas",array("name"=>"7","value"=>"user") );
		$this->System->debug("INSTALL","Sys sql tables created ");
		$modules=$this->System->callmodule("modules");		
		$modules->installation("sys");	
		$modules->installation("base");		
		$modules->installation("users");
		$modules->installation("modules");
		
		/*
		 * Creating administrator account
		 */ 
		$db->insert("users",array(
			"firstname"=>"Administrator",
			"email"=>$this->adminemail,
			"passwd"=>$this->System->encodeString($this->adminpassword),
			"groupid"=>"1",
			"changemod"=>"Y",
			"changeaccess"=>"Y",
			"lastname"=>"",
			"status"  =>0,
			"lastip"  =>"",
			"hash"	  =>"",
			"lastdate"=>date("Y-m-d H:i:s"),
			"createdate"=>date("Y-m-d H:i:s")
		));
		$q=$db->select("actions","hash","",_RESULT_LIST);
		$db->update("groups"," set access='".(implode(",",$q))."' where id='1' ");		
		return true;
	}
	
	public function tablesinstall(&$db){
		$table = new sqltable("files");
		$table->addfield("id",_FIELD_INTAUTO);
		$table->addfield("name",_FIELD_CHAR);
		$table->addfield("type",_FIELD_CHAR);
		$table->addfield("size",_FIELD_INT);
		$table->addfield("source",_FIELD_CHAR);
		$table->addfield("cdate",_FIELD_DATETIME);
		$table->addkey(_KEY_INDEX,"name");
		$db->createtable($table);
		unset($table);
		// table: functions
		$table = new sqltable("actions");
		$table->addfield("hash",_FIELD_CHAR,_HASH_LENGTH);
		$table->addfield("id",_FIELD_INTAUTO);
		$table->addfield("action",_FIELD_CHAR);
		$table->addfield("module",_FIELD_CHAR);
		$table->addfield("guest",_FIELD_INT,2);
		$table->addfield("runmode",_FIELD_INT,2);
		$table->addfield("alias",_FIELD_CHAR);
		$table->addfield("position",_FIELD_INT,2,1);
		$table->addfield("runwith",_FIELD_CHAR,_HASH_LENGTH,"0");
		$table->addfield("withmod",_FIELD_CHAR,"100","0");
		$table->addfield("onstart",_FIELD_INT,null);
		$table->addfield("weight",_FIELD_INT,null);
		$table->addfield("comment",_FIELD_CHAR,"100","");
		$table->addfield("extime",_FIELD_FLOAT);
		$table->addfield("memuse",_FIELD_INT);
		$table->addfield("lastsum",_FIELD_CHAR);
		$table->addkey(_KEY_UNIQUE,"hash");
		$table->addkey(_KEY_INDEX,"runwith");
		$table->addkey(_KEY_INDEX,"onstart");
		$table->addkey(_KEY_INDEX,"alias");
		$db->createtable($table);
		unset($table);

		//table: levellinks
		
		$table = new sqltable("levellinks");
		$table->addfield("hash",_FIELD_CHAR,_HASH_LENGTH);
		$table->addfield("level",_FIELD_INT,3);
		$table->addkey(_KEY_INDEX,"hash");
		$table->addkey(_KEY_INDEX,"level");
		$db->createtable($table);
		unset($table);		

		// table: groups
		
		$table = new sqltable("groups");
		$table->addfield("id",_FIELD_INTAUTO);
		$table->addfield("name",_FIELD_CHAR);
		$table->addfield("access",_FIELD_TEXT);
		$table->addfield("createdate",_FIELD_DATETIME);
		$table->addkey(_KEY_UNIQUE,"name");
		$db->createtable($table);
		unset($table);

		# langterms
		$table = new sqltable("langterms");
		$table->addfield("id",_FIELD_INTAUTO);
		$table->addfield("term",_FIELD_CHAR,_HASH_LENGTH);
                $table->addfield("alias",_FIELD_CHAR);
                $table->addfield("module",_FIELD_CHAR);
                $table->addfield("method",_FIELD_CHAR);
		$table->addfield("lang",_FIELD_CHAR,"3");
		$table->addfield("weburl",_FIELD_CHAR,null,"");
		$table->addfield("value",_FIELD_CHAR,"");
		$table->addfield("image",_FIELD_CHAR,"");
		$table->addfield("url",_FIELD_CHAR,"");
		$table->addfield("param",_FIELD_CHAR,null,"");
		$table->addfield("parent",_FIELD_INT,"0");
		$table->addfield("weight",_FIELD_INT,"0");
		$table->addkey(_KEY_INDEX,"term");
                $table->addkey(_KEY_INDEX,"alias");
                $table->addkey(_KEY_INDEX,"module");
                $table->addkey(_KEY_INDEX,"method");
                $table->addkey(_KEY_INDEX,"lang");
                $table->addkey(_KEY_INDEX,"parent");
                $table->addkey(_KEY_INDEX,"weight");
		//table: log
		$db->createtable($table);
		unset($table);
		
		
		$table = new sqltable("log");
		$table->addfield("id",_FIELD_INTAUTO);
		$table->addfield("title",_FIELD_CHAR);
		$table->addfield("description",_FIELD_TEXT);
		$table->addfield("level",_FIELD_INT);
		$table->addfield("ip",_FIELD_CHAR);
		$table->addfield(_FIELD_DATETIME,_FIELD_DATETIME);
		$db->createtable($table);
		unset($table);

		//table: modules
		$table = new sqltable("modules");
		$table->addfield("modname",_FIELD_CHAR);
		$table->addfield("status",_FIELD_INT,"0");
		$table->addfield("installdate",_FIELD_DATETIME);
		$table->addfield("description",_FIELD_TEXT);
		$table->addfield("version",_FIELD_CHAR);
		$table->addfield("author",_FIELD_CHAR);
		$table->addfield("permissions",_FIELD_CHAR,6,_PERM_DEFAULT);
		$table->addkey(_KEY_PRIMARY,"modname");
		
		$db->createtable($table);
		unset($table);
		// table: system

		$table = new sqltable("systemoptions");
		$table->addfield("id",_FIELD_INTAUTO);
		$table->addfield("keyname",_FIELD_CHAR);
		$table->addfield("value",_FIELD_CHAR);
		$table->addfield("item",_FIELD_CHAR);
		$db->createtable($table);
		unset($table);		
		
		//table: users
		$table = new sqltable("users");
		$table->addfield("id",_FIELD_INTAUTO);
		$table->addfield("firstname",_FIELD_CHAR);
		$table->addfield("lastname",_FIELD_CHAR);
		$table->addfield("email",_FIELD_CHAR);
		$table->addfield("passwd",_FIELD_CHAR);
		$table->addfield("changemod",_FIELD_CHAR,"1","N");
		$table->addfield("changeaccess",_FIELD_CHAR,1,"N");
		$table->addfield("groupid",_FIELD_INT,"0");
		$table->addfield("status",_FIELD_INT,"0");
		$table->addfield("lastip",_FIELD_CHAR);
		$table->addfield("hash",_FIELD_CHAR);
		$table->addfield("lastdate",_FIELD_DATETIME);
		$table->addfield("createdate",_FIELD_DATETIME);
		$table->addkey(_KEY_UNIQUE,"email");

		$db->createtable($table);
		unset($table);
		//table : users_session
		$table = new sqltable("users_session");
		$table->addfield("session_id",_FIELD_CHAR);
		$table->addfield("usertrace",_FIELD_CHAR);
		$table->addfield("online",_FIELD_INT,null);
		$table->addfield("user_id",_FIELD_INT,null);
		$table->addfield("lifetime",_FIELD_BIGINT);
		$table->addfield("last_impression",_FIELD_BIGINT,20);
		$table->addfield("created",_FIELD_BIGINT,20);
		$table->addfield("user_agent",_FIELD_CHAR);		
		$table->addkey(_KEY_PRIMARY,"session_id");
		$db->createtable($table);
		unset($table);

		//table : levels
		$table = new sqltable("levels");
		$table->addfield("id",_FIELD_INTAUTO);
		$table->addfield("name",_FIELD_CHAR);
		$table->addkey(_KEY_UNIQUE,"name");
		
		$db->createtable($table);
		unset($table);		
		
		//table : users_session_variable
		$table = new sqltable("users_session_variable");
		$table->addfield("session_id",_FIELD_CHAR);
		$table->addfield("data",_FIELD_TEXT);
		$table->addkey(_KEY_PRIMARY,"session_id");
	
		$db->createtable($table);
		unset($table);				
		// table: output_areas
		$table = new sqltable("output_areas");
		$table->addfield("name",_FIELD_INTAUTO);
		$table->addfield("value",_FIELD_CHAR);
		$db->createtable($table);
		unset($table);
	}
	
	
	public function cmsinstall(){
		
	}
	
	
}